package com.cmict.data.infra.controller.admin.rdb;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.common.pojo.CommonResult;
import static com.cmict.data.framework.common.pojo.CommonResult.success;

import com.cmict.data.framework.excel.core.util.ExcelUtils;

import com.cmict.data.framework.operatelog.core.annotations.OperateLog;
import static com.cmict.data.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cmict.data.infra.controller.admin.rdb.vo.*;
import com.cmict.data.infra.dal.dataobject.rdb.RdbDO;
import com.cmict.data.infra.convert.rdb.RdbConvert;
import com.cmict.data.infra.service.rdb.RdbService;

@Api(tags = "管理后台 - 数据库")
@RestController
@RequestMapping("/infra/rdb")
@Validated
@RequiredArgsConstructor
public class RdbController {

    @Resource
    private RdbService rdbService;

    @PostMapping("/create")
    @ApiOperation("创建数据库")
    @PreAuthorize("@ss.hasPermission('sys:rdb:create')")
    public CommonResult<Long> createRdb(@Valid @RequestBody RdbCreateReqVO createReqVO) {
        return success(rdbService.createRdb(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新数据库")
    @PreAuthorize("@ss.hasPermission('sys:rdb:update')")
    public CommonResult<Boolean> updateRdb(@Valid @RequestBody RdbUpdateReqVO updateReqVO) {
        rdbService.updateRdb(updateReqVO);
        return success(true);
    }

    @PostMapping("/testRdb")
    @ApiOperation("数据库页面-测试数据库连接")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult<Boolean> testRdb(@Valid @RequestBody RdbTest RdbTest) {
        rdbService.testRdb(RdbTest);
        return success(true);
    }
    @GetMapping("/execute/page")
    @ApiOperation("数据表页面  - 根据sql查询")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult executeSql(@Valid  RdbSqlPage rdbSqlPage) {
        return   rdbService.executeSql(rdbSqlPage);
    }
    @PostMapping("/execute/getColumnList")
    @ApiOperation("数据表页面  - 展开查询")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult getColumnList(@Valid @RequestBody  RdbSql rdbSql) {
        return   rdbService.getColumnList(rdbSql);
    }
    @GetMapping("/execute/getDataList")
    @ApiOperation("数据表页面  - 打开数据表")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult getDataList(@Valid  RdbSql rdbSql) {
        return   rdbService.getDataList(rdbSql);
    }

    @PostMapping("/execute/createTable")
    @ApiOperation("数据表页面  - 创建表")
    @PreAuthorize("@ss.hasPermission('sys:rdb:create')")
    public CommonResult createTable(@Valid @RequestBody RdbCreateTableVo rdbCreateTableVo) {
        return   rdbService.createTable(rdbCreateTableVo);
    }

    @GetMapping("/execute/list")
    @ApiOperation("数据表页面  - 根据sql查询")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult executeSql(@Valid  RdbSql rdbSql) {
        return   rdbService.executeListBySql(rdbSql);
    }

    @GetMapping("/execute/sourceTestList")
    @ApiOperation("数据源页面  - 测试查看")
//    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult sourceTestList(@Valid  RdbSourceSql rdbSourceSql) {
        return   rdbService.sourceTestList(rdbSourceSql);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除数据库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb:delete')")
    public CommonResult<Boolean> deleteRdb(@RequestParam("id") Long id) {
        rdbService.deleteRdb(id);
        return success(true);
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("批量删除数据库")
    @PreAuthorize("@ss.hasPermission('sys:rdb:delete')")
    public CommonResult<Boolean> batchDeleteRdb(@RequestBody Collection<Long> ids) {
        rdbService.batchDeleteRdb(ids);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得数据库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult<RdbRespVO> getRdb(@RequestParam("id") Long id) {
        RdbDO rdb = rdbService.getRdb(id);
        return success(RdbConvert.INSTANCE.convert(rdb));
    }

    @GetMapping("/list")
    @ApiOperation("获得数据库列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult<List<RdbRespVO>> getRdbList(@RequestParam("ids") Collection<Long> ids) {
        List<RdbDO> list = rdbService.getRdbList(ids);
        return success(RdbConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getIpGroupChildren")
    @ApiOperation("数据源 - 引用数据库")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult<Set<RdbRespVO>> getIpGroupChildren() {
        Set<RdbRespVO> list = rdbService.getIpGroupChildren();
        return success(list);
    }

    @GetMapping("/page")
    @ApiOperation("获得数据库分页")
    @PreAuthorize("@ss.hasPermission('sys:rdb:query')")
    public CommonResult<PageResult<RdbRespVO>> getRdbPage(@Valid RdbPageReqVO pageVO) {
        PageResult<RdbDO> pageResult = rdbService.getRdbPage(pageVO);
        return success(RdbConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出数据库 Excel")
    @PreAuthorize("@ss.hasPermission('sys:rdb:export')")
    @OperateLog(type = EXPORT)
    public void exportRdbExcel(@Valid RdbExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RdbDO> list = rdbService.getRdbList(exportReqVO);
        // 导出 Excel
        List<RdbExcelVO> datas = RdbConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "数据库.xls", "数据", RdbExcelVO.class, datas);
    }

}
